﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Scavenger
{
    class Projectile
    {
        /* Class description
         * Thought to be the class for all projectiles.
         * Should include a reference to originating character.
         */
        #region Declarations
        public Texture2D Texture;
        public Vector2 Position;

        public bool Active;
        public int Damage;
        public int Width
        {
            get { return Texture.Width; }
        }
        public int Height
        {
            get { return Texture.Height; }
        }

        Viewport viewport;

        float moveSpeed, rotation;

        //TEMP
        public float dX, dY;

        #endregion

        public void Initialize(Viewport viewport, Texture2D texture, Vector2 position, float rotation)
        {
            this.Texture = texture;
            this.Position = position;
            this.viewport = viewport;
            this.rotation = rotation;
           
            // Should calculate damage based on character who fired the projectile
            this.Damage = 10;

            // Should be determined by ammo type
            this.moveSpeed = 10f;

            this.Active = true;
        }

        public void Update()
        {
            dX = (float)Math.Abs((Math.Sin(rotation) * moveSpeed));
            dY = (float)Math.Abs((Math.Cos(rotation) * moveSpeed));

            if (rotation < MathHelper.Pi)
            {
                Position.X += dX;
            }
            else if (rotation > MathHelper.Pi && rotation < 2 * MathHelper.TwoPi)
            {
                Position.X -= dX;
            }

            if (rotation < MathHelper.PiOver2 || rotation > 3 * MathHelper.PiOver2)
            {
                Position.Y -= dY;
            }
            else if (rotation > MathHelper.PiOver2 || rotation < 3 * MathHelper.PiOver2)
            {
                Position.Y += dY;
            }
                        
            // Disables projectile when out of view(port)
            Rectangle projectileRectangle = new Rectangle((int)Position.X, (int)Position.Y, Width, Height);
            if (!viewport.Bounds.Contains(projectileRectangle))
                Active = false;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle projectileRectangle = new Rectangle((int)Position.X, (int)Position.Y, Width, Height);
            spriteBatch.Draw(Texture, projectileRectangle, null, Color.White, rotation - MathHelper.PiOver2, new Vector2(Width / 2, Height / 2),
                SpriteEffects.None, 0f);
        }

    }
}
